#!\usr\bin\env python

import json
from urllib import request
import urllib
import datetime

def write_log(info):
	with open("spider.log" ,"a") as f:
		f.write("[%s]:\t%s \n\n" % (datetime.datetime.now(),info))

def get_http_data(param, headers):
	data = json.dumps(param) + ""
	data = data.replace(' ','')
	post_url = 'https://www.greatgas.cn/prod/rest/goods/mall/searchByConditions'
	req = request.Request(post_url, data.encode('utf-8'), headers)
	with request.urlopen(req) as f:
		rsp = f.read().decode("utf-8")
		jsonobj = json.loads(rsp)
		return jsonobj


def load_remote_data():
	param = {"arguments":[
		{"conditions":"[]"},
		{"from":"0"},{"pagesize":"1"},
		{"sortField":"dynProperties.goodsPubTime"},
		{"sortType":"desc"}]}

	headers = {
		"Accept": "*/*", 
		"Accept-Encoding": "gzip, deflate",
		"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
		"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.101 Safari/537.36",
		"X-Requested-With": "XMLHttpRequest"
	}

	result = []
	totalnum = 0
	try:
		data = get_http_data(param, headers)
		totalnum = int(data["results"][1]["totalNum"])
		result.extend(data["results"][0]["goodsList"])

	except Exception as e:
		print("error:", e)

	if(totalnum > 0):
		param["arguments"][1]["from"] = str(1)
		param["arguments"][2]["pagesize"] = str(totalnum)
		# print("p2", param)
		try:
			data = get_http_data(param, headers)
			# totalnum = int(data["results"][1]["totalNum"])
			result.extend(data["results"][0]["goodsList"])
			# print("r2:", data["results"][0]["goodsList"])

		except Exception as e:
			print("error:", e)
		# print(param, flush = True)

	print("result:", len(result), flush = True)


if __name__ == '__main__':
	load_remote_data()
